﻿using System;
using System.Web;
using Easy.BLL;
using Easy.Operation;
using Easy.Model;
using Newtonsoft.Json;
using Easy.Common;

namespace Easy.Web.Service.Data
{
    /// <summary>
    /// SoftDeleteItem 的摘要说明
    /// </summary>
    public class SoftDeleteItem : BaseHttpHandler
    {

        public override void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string action = context.Request["action"] ?? "delete";
            string entityName = context.Request["entityname"];

            string Id = context.Request["id"];

            CustomResult cusResult = new CustomResult();

            cusResult.ResultData = Id;
            
            try
            {
                if (action.Trim().ToLower() == "delete")
                {
                    //CURD curd = new CURD(entityName, this.CurrentUser);
                    DataService service = new DataService(entityName, this.CurrentUserId);
                    service.SoftDelete(new Guid(Id));
                    cusResult.ResultCode = 0;
                    cusResult.ResultMessage = "成功";
                    cusResult.ResultData = Id;
                }
                else
                {
                    DataService service = new DataService(entityName, this.CurrentUserId);
                    service.RecoverSoftDelete(new Guid(Id));
                    cusResult.ResultCode = 0;
                    cusResult.ResultMessage = "成功";
                    cusResult.ResultData = Id;
                }
            }
            catch (CustomException ce)
            {
                cusResult = ce.CustomResult;
                cusResult.ResultData = Id;
            }
            catch (Exception e)
            {
                cusResult.ResultCode = -1;
                cusResult.ResultMessage = e.Message;
            }
            finally
            {
                context.Response.Write(JsonConvert.SerializeObject(cusResult));
            }
            
        }


    }
}